#include <stdio.h>
#include <windows.h>
#include <process.h>

#define NUM_THREAD 50

long long num = 0;

unsigned WINAPI Inc(void *arg)
{
    for (int i = 0; i < 50000000; i++)
        num += 1;
    return 0;
}

unsigned WINAPI Dec(void *arg)
{
    for (int i = 0; i < 50000000; i++)
        num -= 1;
    return 0;
}

int main(int argc, char **argv)
{
    int i;
    HANDLE tHandles[NUM_THREAD];

    printf("sizeof long long: %d \n", sizeof(long long));

    for(i = 0; i < NUM_THREAD; i++){
        if(i%2)
            tHandles[i] = (HANDLE)_beginthreadex(NULL, 0, Inc, NULL, 0, NULL);
        else
            tHandles[i] = (HANDLE)_beginthreadex(NULL, 0, Dec, NULL, 0, NULL);
    }

    WaitForMultipleObjects(NUM_THREAD, tHandles, TRUE, INFINITE);
    printf("result: %lld\n", num);

    return 0;
}
